package com.epam.kulibaba.helpers.testSupport;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;

import com.epam.kulibaba.components.PanelCriterias.PanelCornerCriterias;
import com.epam.kulibaba.components.catalog.ProductsCatalog;
import com.epam.kulibaba.components.catalog.navigator.CatalogNavigatorPager;
import com.epam.kulibaba.components.catalog.order.LinksBarOrder;
import com.epam.kulibaba.helpers.Class.Catalog;
import com.epam.kulibaba.helpers.Class.CatalogPage;
import com.epam.kulibaba.helpers.Class.Product;
import com.google.common.collect.Lists;

public class ProductsListHelper {
	
	public static Catalog getAllProductsList(WebDriver driver,
			CatalogNavigatorPager catalogPager,
			CatalogNavigatorPager linkForward, ProductsCatalog productsCatalog) {
		Reporter.log("Get All products list");
		String lp = catalogPager.getLastPage().getText();
		Catalog catalog = new Catalog();
		for (int index = 0; index < Integer.parseInt(lp); index++) {
			if (index > 0)
				CatalogNavigatorPager.goToForwardPage(linkForward);
			productsCatalog = ProductsCatalog.getProductsCatalog(driver);
			CatalogPage catalogPage = new CatalogPage();
			catalogPage.produtcs = ProductsCatalog
					.getProducts(productsCatalog);
			catalog.pages.add(catalogPage);
			catalog.globals.addAll(catalogPage.produtcs);
		}

		return catalog;
	}

	public static void checkSortProductsListByPrice(Catalog catalog) {
		Reporter.log("check Sort Products List By Price");
		List<Product> myProducts = new ArrayList<Product>(catalog.globals);
		SortingEngine.orderBy("price", myProducts);
		for (int i = 0; i < myProducts.size(); i++) {
			Assert.assertEquals(catalog.globals.get(i).getPrice(), myProducts
					.get(i).getPrice(), 0.01);
		}
	}

	public static void checkSortProductsListByName(Catalog catalog) {
		Reporter.log("check Sort Products List By Name");
		List<Product> myProducts = new ArrayList<Product>(catalog.globals);
		SortingEngine.orderBy("name", myProducts);
		for (int i = 0; i < myProducts.size(); i++) {
			Assert.assertEquals(catalog.globals.get(i).getName(), myProducts
					.get(i).getName());
		}
	}

	public static void setPriceFilter(CatalogNavigatorPager firstPage,
			PanelCornerCriterias minPrices, PanelCornerCriterias maxPrices,
			double minPrice, double maxPrice) {
		// TODO: minPrice<maxPrice
		CatalogNavigatorPager.goToFirstPage(firstPage);
		PanelCornerCriterias.setMinPrice(minPrices, minPrice);
		PanelCornerCriterias.setMaxPrice(maxPrices, maxPrice);
	}

	public static int filterPriceCatalog(Catalog catalog, double minPrice,
			double maxPrice) {
		Reporter.log("Sort products in catalog between minimum " + minPrice
				+ " and maximum prices" + maxPrice);
		List<Product> myProducts = new ArrayList<Product>(catalog.globals);
		List<Product> myFilterProducts = new ArrayList<Product>();
		for (Product item : myProducts) {
			if (item.getPrice() >= minPrice && item.getPrice() <= maxPrice) {
				myFilterProducts.add(item);
			}
		}
		// System.out.println("myFilterProducts size = "
		// +myFilterProducts.size());
		return myFilterProducts.size();
	}

	public static void checkFilterPriceProducts(Catalog catalog,
			WebDriver driver, CatalogNavigatorPager catalogPager,
			CatalogNavigatorPager linkForward, ProductsCatalog productsCatalog,
			double minPrice, double maxPrice, PanelCornerCriterias total,
			CatalogNavigatorPager firstPage, PanelCornerCriterias minPrices,
			PanelCornerCriterias maxPrices) {
		catalog = getAllProductsList(driver, catalogPager, linkForward,
				productsCatalog);
		setPriceFilter(firstPage, minPrices, maxPrices, minPrice, maxPrice);
		Reporter.log("Check the correct sort products between minimum "
				+ minPrice + " and maximum prices" + maxPrice);
		Assert.assertEquals(filterPriceCatalog(catalog, minPrice, maxPrice),
				Integer.parseInt(total.getTotal().getText()));
	}
	
}
